package com.itheima.appserver.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.appserver.config.MyThreadLocal;
import com.itheima.appserver.excepiton.ErrInfo;
import com.itheima.appserver.excepiton.ErroResult;
import com.itheima.appserver.excepiton.MyEx;
import com.itheima.commons.constants.Constants;
import com.itheima.commons.pojo.User;
import com.itheima.commons.pojo.UserInfo;
import com.itheima.commons.utils.FaceDetect;
import com.itheima.commons.utils.SendMsgUtil;
import com.itheima.commons.utils.UploadPicUtil;
import com.itheima.dubbo.interfaces.UserApi;
import com.itheima.dubbo.interfaces.UserInfoApi;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    /**
     * 首次登录在数据库中注册信息
     * @param token     用户token
     * @param params    注册信息
     */
    public void loginReginfo(Map<String, String> params) {


        //params值不为空
        if (StrUtil.hasBlank(params.get("gender"),params.get("nickname"),params.get("birthday"),params.get("city"))) {
            throw new MyEx(ErroResult.fail(ErrInfo.userInfoIsEmptyError));
        }

       /* if (!StrUtil.hasBlank(params.get("header"))){
            //校验是否为图片格式
            //校验图片大小
            //校验是否为人脸
            validatePic(params.get("header"));
        }*/

        //把数据封装到UserInfo中，添加至数据库
        UserInfo userInfo = UserInfo.builder()
                .userId(MyThreadLocal.getUserId())
                .nickName(params.get("nickname"))
                .birthday(params.get("birthday"))
                .city(params.get("city"))
                .sex(params.get("gender").equalsIgnoreCase("man") ? 1 : 2)
                .build();

        userInfoApi.addUserInfo(userInfo);
    }

    /**
     * 更新用户头像
     * @param
     * @param pic
     */
    public void UpdateHeadPic(MultipartFile pic) {

        //校验是否为图片格式
        if (!StrUtil.endWithAnyIgnoreCase(pic.getOriginalFilename(),".png", ".jpg", ".jpeg")) {
            throw new MyEx(ErroResult.fail(ErrInfo.picFormatError));
        }
        //校验图片大小
        if (pic.getSize()/1024/1024 >= 2) {
            throw new MyEx(ErroResult.fail(ErrInfo.picSizeError));
        }
        //校验是否为人脸
        try {
            if (!FaceDetect.faceDetect(pic.getBytes())) {
                throw new MyEx(ErroResult.fail(ErrInfo.picError));
            }
            // 上传到阿里云OSS  返回地址
            String pictureURL = UploadPicUtil.uploadPic(pic.getBytes(),
                    StrUtil.subAfter(pic.getOriginalFilename(), ".", true));

            //更新头像至数据库

            userInfoApi.updatePic(MyThreadLocal.getUserId(),pictureURL);
        } catch (IOException e) {
            //图片转字节出错，抛未知异常
            throw new MyEx(ErroResult.fail());
        }
    }


}
